﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using NHibernate;
using HP.Cachalote.DataModels;
using HP.Cachalote.Repositories.Interfaces;

namespace HP.Cachalote.Repositories.NHibernate
{
    /// <summary>
    /// An instance of IRepository against the specific DataModel type and provide standard CRUD operation only.
    /// </summary>
    /// <typeparam name="T">The specific DataModel type.</typeparam>
    public abstract class RepositoryBase<T> : IRepository<T> where T : DataModelBase
    {
        protected ISession Session;

        /// <summary>
        /// Just need to make sure every sub-class has a contructor accpets ISession parameter.
        /// </summary>
        /// <param name="session">An instance implements ISession.</param>
        public RepositoryBase(ISession session)
        {
            Session = session;
        }

        /// <summary>
        /// Add new entity.
        /// </summary>
        /// <param name="entity">The new entity will be added into database.</param>
        /// <returns>The id of the new added entity.</returns>
        public virtual object Add(T entity)
        {
            return (int)Session.Save(entity);
        }

        // <summary>
        /// Get entity by id.
        /// </summary>
        /// <param name="id">The specified id.</param>
        /// <returns>The matched data.</returns>
        public virtual T Get(object id)
        {
            return Session.Get<T>(id);
        }

        /// <summary>
        /// Get entity by id. It will never return null. It will always return an entity or throw an exception.
        /// It is free to return a proxy instead. If you know that the value exist in the database, 
        /// and you don’t want to pay the extra select to have that, 
        /// but you want to get that value so we can add that reference to an object.
        /// </summary>
        /// <param name="id">The specified id.</param>
        /// <returns>The matched data.</returns>
        public virtual T Load(object id)
        {
            return Session.Load<T>(id);
        }

        /// <summary>
        /// Filter data by lambda expression.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts T and return boolean.</param>
        /// <returns>An IQueryable list of data.</returns>
        public virtual IQueryable<T> Filter(Expression<Func<T, bool>> expression)
        {
            return Session.QueryOver<T>().Where(expression).Future().AsQueryable();
        }

        /// <summary>
        /// Get all data. You also can filter the data later since the returned result is a IQueryable collection.
        /// </summary>
        /// <returns>An IQueryable collection of data.</returns>
        public virtual IQueryable<T> GetAll()
        {
            return Session.QueryOver<T>().Future().AsQueryable();
        }

        /// <summary>
        /// Get the data's count by using provided lambda expression.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts T and return boolean.</param>
        /// <returns>The data's count.</returns>
        public virtual int GetRowCount(Expression<Func<T, bool>> expression)
        {
            return Session.QueryOver<T>().Where(expression).RowCount();
        }

        /// <summary>
        /// Update existing entity.
        /// </summary>
        /// <param name="entity">The entity will be updated.</param>
        public virtual void Update(T entity)
        {
            Session.Update(entity);
        }

        /// <summary>
        /// Add or update entity.
        /// </summary>
        /// <param name="data">The entity will be updated if it is existing or added if it cannot be found.</param>
        public virtual void AddOrUpdate(T entity)
        {
            Session.SaveOrUpdate(entity);
        }

        /// <summary>
        /// Delete the specified entity.
        /// </summary>
        /// <param name="entity">The entity will be deleted from database.</param>
        public virtual void Delete(T entity)
        {
            Session.Delete(entity);
        }
    }
}
